home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / dev / gcc / newgccstart.lha / source.lha / libm / start.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  2.9 KB  |  134 lines

  1. #ifdef __GNUC__
  2. #include <inline/exec.h>
  3. #include <dos/dos.h>
  4. #include <inline/dos.h>
  5. #endif
  6. #ifndef __GNUC__
  7. #include <clib/exec_protos.h>
  8. #include <clib/dos_protos.h>
  9. #endif
  10. #include <exec/memory.h>
  11.  
  12. extern struct WBStartup *_WBenchMsg;
  13. extern int main(int argc,char *argv[]);
  14. extern void *__mallocbase;
  15. extern void *__stdiostreams;
  16. extern void *__otherstreams;
  17. extern void *__atexitbase;
  18. extern void *__endlabel;
  19.  
  20. static char **argv=NULL;  /* You know */
  21. static char *myname=NULL; /* Who am I ? = argv[0] */
  22. static char *cline=NULL;  /* Copy of commandline = argv[1..argc-1] */
  23.  
  24. static char *getmyname(void)
  25. {
  26.   char *a;
  27.   int l;
  28.   for(l=256;;l+=256)
  29.   {
  30.     a=(char *)AllocVec(l,MEMF_ANY); /* Someone else cleans up for us */
  31.     if (a==NULL)
  32.       return NULL;
  33.     if (GetProgramName(a,l))
  34.       return a;
  35.     FreeVec(a);
  36.   }
  37. }
  38.  
  39. static int argsize(char *string)
  40. {
  41.   int i=0;
  42.   while (*string++!='\n')
  43.     i++;
  44.   return i;
  45. }
  46.  
  47. #ifdef __GNUC__
  48. volatile void exit(int returncode)
  49. #else
  50. void exit(int returncode)
  51. #endif
  52. {
  53.   if(&__atexitbase+1!=&__endlabel)
  54.     (*((void (**)(void))&__atexitbase)[1])(); /* Alle Funktionen ausführen */
  55.   if(&__otherstreams+1!=&__atexitbase)
  56.     (*((void (**)(void))&__otherstreams)[1])(); /* Alle Files Schließen */
  57.   if(&__stdiostreams+1!=&__otherstreams)
  58.     (*((void (**)(void))&__stdiostreams)[2])(); /* stdin, stdout, stderr */
  59.   if(&__mallocbase+1!=&__stdiostreams)
  60.     (*((void (**)(void))&__mallocbase)[1])(); /* Allozierten Speicher freigeben */
  61.   if(myname!=NULL)
  62.     FreeVec(myname);
  63.   if(argv!=NULL)
  64.     FreeVec(argv);
  65.   if(cline!=NULL)
  66.     FreeVec(cline);
  67.   _exit(returncode);
  68. }
  69.  
  70. static int wbstart(void)
  71. { return main(0,(char **)_WBenchMsg); }
  72.  
  73. static int clistart(char *commandline)
  74. {
  75.   int i;
  76.   char *b;
  77.   int argc;
  78.   if((cline=(char *)AllocVec(argsize(commandline)+1,MEMF_ANY))==NULL)
  79.     return RETURN_FAIL;
  80.   b=cline;
  81.   argc=1;
  82.   while(*commandline!='\n')
  83.   {
  84.     if(*commandline!='\"')
  85.       while(*commandline!='\n'&&*commandline!=' '&&*commandline!='\t')
  86.     *b++=*commandline++;
  87.     else
  88.     { commandline++;
  89.       while(*commandline!='\n'&&*commandline!='\"')
  90.       {
  91.     if(*commandline=='*')
  92.       if(*++commandline=='\n')
  93.         break;
  94.     *b++=*commandline++;
  95.       }
  96.     }
  97.     if(*commandline!='\n')
  98.     {
  99.       commandline++;
  100.       while(*commandline==' '||*commandline=='\t')
  101.     commandline++;
  102.     }
  103.     *b++='\0';
  104.     argc++;
  105.   }
  106.   if((argv=(char **)AllocVec(argc*sizeof(char *),MEMF_ANY))==NULL)
  107.     return RETURN_FAIL;
  108.   if((myname=getmyname())==NULL) /* exit cleans up for us */
  109.     return RETURN_FAIL;
  110.   argv[0]=myname;
  111.   b=cline;
  112.   for (i=1;i<argc;i++)
  113.   {
  114.     argv[i]=b;
  115.     while(*b++)
  116.       ;
  117.   }
  118.   return main(argc,argv);
  119. }
  120.  
  121. #ifdef __GNUC__
  122. volatile void _main(char *commandline)
  123. #else
  124. void _main(char *commandline)
  125. #endif
  126. {
  127.   if(&__stdiostreams+1!=&__otherstreams)
  128.     (*((void (**)(void))&__stdiostreams)[1])(); /* stdin, stdout, stderr initialisieren */
  129.   if(_WBenchMsg!=NULL)
  130.     exit(wbstart());
  131.   else
  132.     exit(clistart(commandline));
  133. }
  134.